package com.wac.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.entity.Message;
import com.wac.data.mapper.MessageMapper;
import com.wac.data.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wac.data.service.UserService;
import com.wac.data.utils.DataListUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

	@Resource
	private MessageMapper messageMapper;
	@Resource
	private UserService userService;

	private Page<Message> thePage;

	public Page getInfo(Page<Message> page) {
		page.getRecords().forEach(
				message -> {
					message.setSender(userService.getUserSimpleInfo(message.getSendId()));
					message.setReceiver(userService.getUserSimpleInfo(message.getReceiveId()));
				}
		);
		return page;
	}

	@Override
	public Map<String, Object> selectByUserId(int page, String id, int state) {
		thePage = new Page<>(page, 5);
		if (state == -1) {
			messageMapper.selectPage(thePage, new QueryWrapper<Message>().eq("receive_id", id).orderByDesc("time"));
			return new DataListUtil().data(getInfo(thePage));
		}
		messageMapper.selectPage(thePage, new QueryWrapper<Message>().eq("receive_id", id).eq("state", state).orderByDesc("time"));
		return new DataListUtil().data(getInfo(thePage));
	}

	@Override
	public int hasReadAll(String id) {
		return messageMapper.hasReadedAll(id);
	}
}
